<?php
use JPush\Client as JPush;
use \think\Db;

function push($uid,$param = ['type'=>'','message'=>'','parameters'=>'','status'=>'']){
    /*
     *
     * type 1 = 用户，2=司机
     * message  消息
     * parameters 订单号或其他一些参数
     * status 1 = 司机提现结果 ,2 = 用户认证结果,3 = 用户退款,4 =司机抢单提醒, 5= 用户订单提醒,6=获得优惠券提醒,7=私人订单（调度）,8=系统后台推送消息
     *
     */
    try{
        vendor('jpush.autoload');

        // $type = Db::table('user')->where('id',$uid)->value('isExpress');
		$type =  json_decode($param,true);
		$type = $type['type'];
        $app = Db::table('push_config')->where('id',$type)->find();
		
        $client = new JPush($app['appkey'], $app['appsecret']);
        $response = $client->push();
        if($uid != ''){
            $response->addAlias($uid);
        }else{
            $response->addAllAudience();
        }
        $response->setPlatform(array('ios', 'android'))->message($param)->send();
         return true;
    }catch (\JPush\Exceptions\APIConnectionException $e) {
         return false;
    }catch (\JPush\Exceptions\APIRequestException $e) {
         return false;
    }
}

function push_driver($uid,$param = ['type'=>'','message'=>'','parameters'=>'','status'=>'']){
   try{
        vendor('jpush.autoload');
        $app = Db::table('push_config')->where('id',2)->find();
        $client = new JPush($app['appkey'], $app['appsecret']);
        $response = $client->push();
        if($uid != ''){
            $response->addAlias($uid);
        }else{
            $response->addAllAudience();
        }
        $response->setPlatform(array('ios', 'android'))->message($param)->send();
        return true;
    }catch(\JPush\Exceptions\APIConnectionException $e) {
         return false;
    }catch(\JPush\Exceptions\APIRequestException $e) {
        return false;
    }
}

function insert_message($uid,$title = '', $message = '', $type = '', $parameters = '')
{
    /*
     *
     * title = 标题
     * type 1 = 跑男(司机)提现结果 ,2 = 用户认证结果,3 = 用户退款,4 = 跑男（司机）抢单提醒, 5=用户订单提醒,6=获得优惠券提醒,7=私人订单（调度）
     * message  消息
     * parameters 订单号或其他一些参数
     *
     */
    $time = time();
    Db::table('message')->insert(['uid'=>$uid,'title'=>$title,'message'=>$message,'type'=>$type,'parameters'=>$parameters,'time'=>$time]);
}